export function useConcurrence(options, maxLen, reqFunc, cb) {
    return new Promise(resolve => {
        if (!Array.isArray(options)) throw new Error('options type must be [Array]')
        const { length: MAX } = options
        let index = 0
        let count = 0
        const result = []

        async function request() {
            if (index === MAX) return
            let curIndex = index
            index++
            const { payload, hook } = options[curIndex]
            try {
                const resp = await reqFunc(...payload)
                result[curIndex] = resp
                cb && cb(resp, hook)
            } catch (error) {
                result[curIndex] = error
                cb && cb(error, hook)
            } finally {
                count++
                if (count < MAX) {
                    await request()
                } else {
                    resolve(result)
                }
            }
        }
        maxLen = Math.min(maxLen, MAX)
        for (let i = 0; i < maxLen; i++) {
            request()
        }
    })
}
